<template>
	<view>
	<!-- 	<video id="video" :controls="controls" :src="durl" :poster="imgUrl"
			@loadedmetadata="videoUrl">
		</video> -->
		<view class="progress" v-if="showProgress">
			<u-line-progress :percentage="percentage" activeColor='#5490FF' />
		</view>
		<view class="short_video_link">
			<view class="addAudio" contenteditable="true">
				<textarea class="input" placeholder="粘贴复制短视频链接" v-model="link"></textarea>
				<button class="clearButton" @click="clearText">清 空</button>
				<view class="poster" @click="poster">
					粘贴文本
				</view>
			</view>
			<view class="addAudio" v-if="shortRes">
				<textarea class="input" maxlength="-1" placeholder="文案提取结果" v-model="shortRes"></textarea>
			</view>
			<view class="button" v-if="!shortRes">
				<view class="button_center">
					<u-button text="提取文案" color="#5490FF" size="large" @click="shortExtraction"></u-button>
				</view>

			</view>
			<view class="footr_text">温馨提示:文案提取只支持快手，抖音视频链接（不支持图文作品）视频越长时间越慢 提取过程请勿退出</view>
			<view class="button" v-if="shortRes">
				<view class="button_left">
					<u-button text="重新提取" color="#5490FF" size="large" @click="shortExtraction"></u-button>
				</view>
				<view class="button_right">
					<u-button text="复制文案" color="#1EB930" size="large" @click="shortCopy"></u-button>
				</view>
			</view>
		</view>
		<u-modal :show="vipShow" :title="'温馨提示'" :content="'您还不是会员,此功能需开通会员才可使用。'" cancelText="取消" confirmText="去开通"
			showCancelButton="true" @cancel="cancel" @confirm="govip" :closeOnClickOverlay="true"
			@close="vipShow = false"></u-modal>

	</view>
</template>
<script>
	import {
		sslwxbdApi,
		apiMixUrlApi
	} from '@/api/assemb.js';
	import {
		isLogin,
		vipType
	} from '@/business/login.js';
	import {
		uploadAudioFile
	} from '@/utils/upload/index.js';
	import {
		audioTypeJudgment
	} from '@/business/audioTypeJudgment.js';
	let app = getApp();
	const innerAudioContext = uni.createInnerAudioContext(); // 创建内部 audio
	const manager = uni.getFileSystemManager();
	export default {
		data() {
			return {
				video:false,
				timer: null,
				timer1: null,
				percentage: 0,
				showProgress: false,
				vipShow: false,
				value: 1,
				link: '',
				shortRes: '',
				Audio: {
					audioUrl: '',
					audioName: '',
					audioTime: 0,
					isPlay: 0, //0暂停，1播放
					duration: 0 // 总时长
				},
				localRes: '',
				txtPath: '',
				txtName: '',
				txtTime: '',
				txtRes: '',
				videoTime:'',
				durl:''
			}
		},
		onShow() {
			this.isVip = uni.getStorageSync('vipType').vipStatus == 1 ? true : false;
			// this.isVip = true;
		},
		onUnload() {
			clearInterval(this.timer);
			clearInterval(this.timer1);

		},
		//时间戳的处理    
		filters: {
			formatDate: function(value) {
				var date = new Date();
				//date.setTime(value);
				var month = date.getMonth() + 1;
				var hours = date.getHours();
				if (hours < 10)
					hours = "0" + hours;
				var minutes = date.getMinutes();
				if (minutes < 10)
					minutes = "0" + minutes;
				var time = date.getFullYear() + "-" + month + "-" + date.getDate() +
					" " + hours + ":" + minutes;
				return time;
			}

		},
		methods: {
			poster(){
				let that = this;
				uni.getClipboardData({
					success: function(res) {
						that.link = res.data
					}
				});
			},
			videoUrl(e) {
				console.log('11111')
				this.videoTime = e.target.duration
				console.log(this.videoTime)
			},
			txtCopy() {
				uni.setClipboardData({
					data: this.txtRes,
					showToast: true
				});
			},
			txtExtraction() {
				const _this = this
				if (_this.txtPath == '') {
					uni.showToast({
						title: '请添加txt文件',
						duration: 2000,
						icon: 'none'
					});
				} else if (_this.isVip) {
					if (_this.txtPath.match(/\.[^.]+?$/)[0] == '.txt') {
						manager.readFile({
							filePath: _this.txtPath,
							encoding: 'utf-8',
							success: (res) => {
								console.log(res.data);
								_this.txtRes = res.data
							}
						})
					} else {
						uni.showToast({
							title: '请选择正确的txt文件',
							icon: 'none',
							duration: 2000
						})
					}

				} else {
					_this.vipShow = true
				}

			},
			uploadTxt() {
				let _this = this;
				wx.chooseMessageFile({
					count: 1, //默认100
					type: 'file',
					success: function(res) {
						console.log(res);
						_this.txtName = res.tempFiles[0].name
						_this.txtTime = res.tempFiles[0].time
						_this.txtPath = res.tempFiles[0].path
					}
				});

			},
			//本地视频提取
			localCopy() {
				uni.setClipboardData({
					data: this.localRes,
					showToast: true
				});
			},
			localExtraction() {
				const _this = this
				_this.percentage = 0
				if (_this.Audio.audioUrl == '') {
					uni.showToast({
						title: '请添加音视频',
						duration: 2000,
						icon: 'none'
					});
				} else if (_this.isVip || uni.getStorageSync('frequencyrs') > 0) {
					let prams = {
						url: _this.Audio.audioUrl
					};
					apiMixUrlApi(prams, '1078', 'VideoParse').then(result => {
						if (result.code == 200) {
							_this.showProgress = true
							_this.percentage = uni.$u.range(0, 100, _this.percentage + 30)
							console.log(result, '111');
							_this.extractingText(result.data.url, 1)
						} else {
							uni.showToast({
								title: result.descinfo,
								icon: 'none',
								duration: 1500
							});
						}
					})

				} else {
					_this.vipShow = true;
				}
			},
			//短视频链接提取
			shortCopy() {
				uni.setClipboardData({
					data: this.shortRes,
					showToast: true
				});
			},
			shortExtraction() {
				const _this = this

				if (_this.link.trim() == '') {
					uni.showToast({
						title: '请输入链接',
						duration: 2000,
						icon: 'none'
					});
				} else if (_this.isVip || uni.getStorageSync('frequencyrs') > 0) {
					let prams = {
						url: _this.link
					};
					tt.showLoading({
						title: '加载中请稍后',
						mask:true
					})
					apiMixUrlApi(prams, '1078', 'VideoParse').then(result => {
						console.log(result,'0000000000000')
						_this.durl = result.data.url
						if (result.code == 200) {
							_this.extractingText(result.data.url, 0)
							tt.showLoading({
								title: '上传中,请稍等',
								mask:true
							})
						} else {
							uni.showToast({
								title: result.descinfo,
								icon: 'none',
								duration: 1500
							});
						}
					})

				} else {
					_this.vipShow = true;
				}
			},
			extractingText(url, type) {
				const _this = this
				if (!_this.isVip) { // uni.showLoading({
					let param = {
						consumecoins: 1,
						descinfo: '文案提取消耗',
						consumetype: 1,
					}
					apiMixUrlApi(param, '1043', "ConsumeCoins").then(result => {
						console.log(result, '1043返回值');
						uni.setStorageSync('frequencyrs', result.coins)
					});
				}
				let prams = {
					fileLink: url
				}
				apiMixUrlApi(prams, '1087', 'SubmitFileTrans').then(json => {
					if (type == 0) {
						_this.geoip(json.taskId)
					} else if (type == 1) {
						_this.geoipLocal(json.taskId)
					}
				})
			},
			/* local结果查询 */
			geoipLocal(taskId) {
				const _this = this
				let prams = {
					taskId
				}

				apiMixUrlApi(prams, '1088', 'GetAcsResponse').then(json => {

					if (json.descinfo == 'RUNNING') {
						clearInterval(_this.timer1)
						_this.timer1 = setInterval(function() {
							_this.geoipLocal(prams.taskId)
						}, 5000)

					}
					if (json.descinfo == 'SUCCESS') {
						clearInterval(_this.timer1)
						_this.localRes = json.text.replace(/\[[^\]]*\]/g, "").trim()
						_this.showProgress = false

					}
				})
			},
			/* link结果查询 */
			geoip(taskId) {
				const _this = this
				let prams = {
					taskId
				}

				apiMixUrlApi(prams, '1088', 'GetAcsResponse').then(json => {
					if (json.descinfo == 'RUNNING') {
						tt.showLoading({
							title: '提取中,请稍后',
							mask:true
						})
						clearInterval(_this.timer)
						_this.timer = setInterval(function() {
							_this.geoip(prams.taskId)
						}, 5000)
					}
					if (json.descinfo == 'SUCCESS') {
						clearInterval(_this.timer)
						_this.shortRes = json.text.replace(/\[[^\]]*\]/g, "").trim()
						_this.showProgress = false
					}
				})
			},
			clearText() {
				this.link = ''
			},
			govip() {
				this.vipShow = false;
				if (uni.getStorageSync('OS') !== 'ios') {
					uni.navigateTo({
						url: '/pages/paymentCenter/paymentCenter'
					});
				} else {
					uni.showModal({
						title: '提示',
						content: '由于相关规范，IOS功能暂不可用',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			cancel() {
				this.vipShow = false
			},
			changeIndex(e) {
				console.log('选中:', e)
			},
			WaddAudio() {
				let _this = this;
				uploadAudioFile().then(res => {
					_this.localRes = ''
					let Audio = {
						audioName: res.audioName,
						audioUrl: res.audioUrl,
						audioTime: res.audioTime,
						isPlay: 0, //0暂停，1播放
						importWay: 1
					};
					_this.Audio = Audio;
					audioTypeJudgment(res.audioUrl).then(ser => {
						_this.audioIndex = 1;
						if (!!ser) {
							_this.uploadAudioLink = ser;
							_this.audioPlaybackLink = _this.uploadAudioLink;
							_this.play();
						} else {
							_this.uploadAudioLink = res.audioUrl;
							_this.audioPlaybackLink = _this.uploadAudioLink;
							_this.play();
						}
					});
				});
			},
			/*
			 *	@	初始音乐播放
			 */
			importPlay(e) {
				this.audioIndex = e;
				this.audioPlaybackLink = this.uploadAudioLink;
				this.play();
			},
			/**
			 * 音频 播放
			 */
			play() {
				if (this.Audio.isPlay != this.audioIndex) {
					this.Audio.isPlay = this.audioIndex;
					innerAudioContext.src = this.audioPlaybackLink;
					innerAudioContext.play();
				} else if (this.Audio.isPlay == this.audioIndex) {
					innerAudioContext.pause();
					this.Audio.isPlay = 0;
				}
			},
		}
	}
</script>
<style lang="scss">
	/deep/.tab-bar::after {
		height: 0rpx !important;
	}

	.footr_text {
		font-size: 13px;
		margin-top: 10px;
		margin-left: 25px;
	}
    .poster{
		position: absolute;
		 bottom: 13px;
		 font-size: 15px;
		 left: 10px;
		 color: #333;
		
	}
	.txt {
		.txt_box {
			margin: auto;
			width: 676rpx;
			height: 211rpx;
			background: #ffffff;
			border: 1rpx solid #5490FF;
			border-radius: 18rpx;
			display: flex;
			align-items: center;
			justify-content: space-evenly;

			.AudioCentre {
				width: 325rpx;

				.AudioCentre_top {
					width: 260rpx;
					height: 54rpx;
					line-height: 54rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					font-size: 33rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
				}

				.AudioCentre_bottom {
					width: 260rpx;
					height: 54rpx;
					line-height: 54rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					font-size: 22rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #a8a8a8;
				}
			}

			.AudioFile_right {
				width: 157rpx;
				height: 54rpx;
				background: #ffffff;
				border: 2rpx solid #5490FF;
				border-radius: 27rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #5490FF;
				text-align: center;
				line-height: 54rpx;
			}
		}

		.videoAdd {
			width: 679rpx;
			height: 200rpx;
			background: #ffffff;
			border: 1rpx dashed #5490FF;
			border-radius: 18rpx;
			margin: auto;

			.videoAdd_image {
				width: 58rpx;
				height: 58rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #5490FF;
				margin: auto;
				margin-top: 50rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.text {
				font-size: 29rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #5490FF;
				width: 100%;
				text-align: center;
				margin-top: 20rpx;
			}
		}
	}

	.local_video {
		margin-top: 20rpx;

		.addLocalAudio {
			display: flex;
			justify-content: center;
		}
	}

	.progress {
		padding: 0 40rpx;
	}

	.button {
		margin-top: 62rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;

		.button_left {
			width: 47%;
			padding: 0 35rpx;
		}

		.button_right {
			position: relative;
			width: 47%;
			padding: 0 35rpx;

			.right_other {
				display: flex;
				align-items: center;
				position: absolute;
				top: -21rpx;
				right: 0;
				width: 154rpx;
				height: 42rpx;
				background: #FF6600;
				border-radius: 21rpx 0rpx 0rpx 21rpx;

				text {
					line-height: 42rpx;
					margin-left: 18rpx;
					font-size: 18rpx;
					font-family: PingFang SC;
					color: #FFFFFF;
				}
			}
		}

		.button_center {
			width: 90%;
			margin: auto;
		}
	}

	.addAudio {
		width: 678rpx;
		height: 340rpx;
		margin: 0 auto;
		margin-top: 30rpx;
		border: 1px solid #5490FF;
		position: relative;
		overflow: hidden;
		border-radius: 18rpx;

		.input {
			width: 620rpx;
			margin: 0 auto;
			color: #999999;
			overflow: visible;
			word-break: break-all;
			padding: 20rpx 35rpx;
			vertical-align: top;
			outline: none;
		}

		.clearButton {
			line-height: 55rpx;
			font-size: 28rpx;
			color: #ffffff;
			background: #999992;
			text-align: center;
			position: absolute;
			bottom: 20rpx;
			right: 20rpx;
			z-index: 33;
		}
	}
</style>